package com.jt.tenant.activity;

import android.os.Bundle;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.jt.tenant.R;

/**
 * Created by Administrator on 2016/3/11.
 */
public class CarLocationActivity extends JTActivity implements LocationSource {

    private TextView title_name;

    private String longitude,latitude;//经度,纬度

    private TextureMapView mapView;

    private AMap aMap;

    private Marker mGPSMarker;

    private String location;//标题名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_car_location);

        mapView = (TextureMapView) findViewById(R.id.map_map_view);

        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        location = getIntent().getStringExtra("location");

        longitude = getIntent().getStringExtra("longitude");

        latitude = getIntent().getStringExtra("latitude");

        init_view();
    }

    private void init_view() {

        (title_name = (TextView) findViewById(R.id.tv_car_location_title)).setText(location);

        Double d_result1 = Double.parseDouble(longitude);

        Double d_result2 = Double.parseDouble(latitude);

        LatLng latLng = new LatLng(d_result1,d_result2);

        aMap.setLocationSource(this);// 设置定位监听

        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));

        MarkerOptions markOptions = new MarkerOptions();

        mGPSMarker = aMap.addMarker(markOptions.position(latLng));

        updateLocation(d_result2, d_result1);

    }

    /*
     * 根据新的经纬度更新GPS位置和设置地图中心   纬度  经度
	 */
    private void updateLocation(double latitude, double longtitude) {

        if (mGPSMarker != null) {

            LatLng latLng = new LatLng(latitude, longtitude);

            mGPSMarker.setPosition(latLng);

            CameraUpdate update = CameraUpdateFactory.changeLatLng(latLng);

            mapView.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longtitude), 13));

        }

    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {

    }

    @Override
    public void deactivate() {

    }
}
